/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Graph;

import static BuildGrid.Search.k;
import static Edge.BuildEdge.column;
import static Edge.BuildEdge.row;
import static Edge.BuildEdge.width;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;

/**
 *
 * @author 江南才俊周涛涛
 */
public class Vertex {

    public int Vertex_name;
    public double x;//结点坐标
    public double y;
    public boolean isactive = false;
    public boolean isvertex = false;
    public int border_name;//边界点名字，在添加边界点是，避免重复添加
    public boolean isborder = false;//判断是否已经判断为边界点
    private List<Parents> parents = new ArrayList<Parents>(5);//父母结点，一个Vertex最多在四个格子中（十字交点），所以提前赋值5空间
    public int grid_index;//在格子中的下标
    public List<Car> cars = new ArrayList<Car>();
    public List<Car> kcar = new ArrayList<Car>();
    public double max_dis = Double.MAX_VALUE;
    public HashSet<Vnode> neighbors=new HashSet<Vnode>();

    public Vertex(int Vertex_name, double x, double y) {
        this.Vertex_name = Vertex_name;
        this.x = x;
        this.y = y;
    }

    public Vertex(Vertex temp) {
        this.Vertex_name = temp.Vertex_name;
        this.x = temp.x;
        this.y = temp.y;
    }

    //获取Vertex父母
    public void setparents() {
        if (parents.isEmpty()) {
            int i, j;//i为行号，j为列号
            i = (int) y / width;
            j = (int) x / width;

            if (isvertex) {
                if (x == j * width && y == i * width) {
                    if (i < row && j < column) {
                        Parents parent = new Parents(i, j);
                        parents.add(parent);
                        return;
                    }
                    if (i > 0 && j < column) {
                        Parents parent = new Parents(i - 1, j);
                        parents.add(parent);
                        return;
                    }
                    if (j > 0 && i < row) {
                        Parents parent = new Parents(i, j - 1);
                        parents.add(parent);
                        return;
                    }
                    if (i > 0 && i < row && j > 0 && j < column) {
                        Parents parent = new Parents(i - 1, j - 1);
                        parents.add(parent);
                        return;
                    }
                } else if (x == j * width) {
                    if (j > 0) {
                        Parents parent = new Parents(i, j - 1);
                        parents.add(parent);
                        return;
                    }
                    if (j < column) {
                        Parents parent = new Parents(i, j);
                        parents.add(parent);
                        return;
                    }
                } else if (y == i * width) {
                    if (i > 0) {
                        Parents parent = new Parents(i - 1, j);
                        parents.add(parent);
                        return;
                    }
                    if (i < row) {
                        Parents parent = new Parents(i, j);
                        parents.add(parent);
                        return;
                    }
                } else {
                    Parents parent = new Parents(i, j);
//                    parent.x=i;
//                    parent.y=j;
                    parents.add(parent);
                    return;
                }
                return;
            }

            if (x == j * width && y == i * width) {
                if (i < row && j < column) {
                    //parents.add(row * i + j);
                    Parents parent = new Parents(i, j);
                    parents.add(parent);
                }
                if (i > 0 && j < column) {
//                    parents.add(row * (i - 1) + j);
                    Parents parent = new Parents(i - 1, j);
                    parents.add(parent);
                }
                if (j > 0 && i < row) {
//                    parents.add(row * i + j - 1);
                    Parents parent = new Parents(i, j - 1);
                    parents.add(parent);
                }
                if (i > 0 && i < row && j > 0 && j < column) {
//                    parents.add(row * (i - 1) + j - 1);
                    Parents parent = new Parents(i - 1, j - 1);
                    parents.add(parent);
                }
            } else if (x == j * width) {
                if (j > 0) {
//                    parents.add(i * row + j - 1);
                    Parents parent = new Parents(i, j - 1);
                    parents.add(parent);
                }
                if (j < column) {
//                    parents.add(i * row + j);
                    Parents parent = new Parents(i, j);
                    parents.add(parent);
                }
            } else if (y == i * width) {
                if (i > 0) {
//                    parents.add((i - 1) * row + j);
                    Parents parent = new Parents(i - 1, j);
                    parents.add(parent);
                }
                if (i < row) {
//                    parents.add(i * row + j);
                    Parents parent = new Parents(i, j);
                    parents.add(parent);
                }
            } else {
//                parents.add(i * row + j);
                Parents parent = new Parents(i, j);
                parents.add(parent);
            }

        }
    }

    //返回父母
    public List<Parents> getParents() {
        setparents();
        return parents;
    }

    //添加汽车并排序(活跃点)
    public void addCar(Car c) {
        cars.add(c);
        Collections.sort(cars, new CostComprator());
    }
    
    public void deleteCar(Car c){
        cars.remove(c);
        if(cars.isEmpty()){
            isactive=false;
        }
    }

    public void add_kCar(Car c) {
        kcar.add(c);
        Collections.sort(cars, new CostComprator2());
    }

    static class CostComprator implements Comparator {

        public int compare(Object object1, Object object2) {// 实现接口中的方法  
            Car p1 = (Car) object1; // 强制转换  
            Car p2 = (Car) object2;
            return Double.compare(p1.dis, p2.dis);
        }
    }

    static class CostComprator2 implements Comparator {

        public int compare(Object object1, Object object2) {// 实现接口中的方法  
            Car p1 = (Car) object1; // 强制转换  
            Car p2 = (Car) object2;
            return Double.compare(p1.query_dis, p2.query_dis);
        }
    }

}
